/*
function binderMenuBuilder() {
	var popup = document.getElementById('binder-selection-menu');
	clearChildren(popup);
	getBinders(function a(arr) {
		if (arr != null)
			// TODO need to create some sort of a record or keep the list around
			for ( var i = 0; i < arr.length; i++) {
				var mi = addNewElement(popup, "menuitem", arr[i].title, arr);
				mi.setAttribute("oncommand", "activateBinder('" + arr[i].id + "')");
			}
	};
}*/

function cannotGetBinderList() {
	var popup = document.getElementById('binder-selection-menu');
	clearChildren(popup);
	var item = document.createElementNS(XUL_NS, "menuitem");
	item.setAttribute("label", "No data");
	popup.appendChild(item);
}

function activateBinder(id) {
	getBinders(function a(arr) {
		if (arr != null) {
			var links = arr[0].links;
			for ( var i = 0; i < links.length; i++)
				openPage(links[i]);
		}
	}, id);
}

var keyHelper = {
	prefs : null,

	startup : function() {
		// Do we need to remove the listener?
		window.removeEventListener("load", onLoadHelper, false);
		this.updateKeys();
		// this.prefs.addObserver("", this, false);
	},

	shutdown : function() {
		// this.prefs.removeObserver("", this);
	},

	// The new shortcuts do not kick in until next restart anyway
	// observe : function(subject, topic, data) {
	// // alert(subject + "\n" + topic + "\n" + data);
	// if (topic != "nsPref:changed") return;
	// this.updateKeys();
	// },

	updateKeys : function() {
		if (this.prefs == null)
			this.prefs = Components.classes["@mozilla.org/preferences-service;1"]
					.getService(Components.interfaces.nsIPrefBranch);
		var key = window.document.getElementById("key_openFireSidebar");

		var ctrl = this.prefs.getBoolPref("firebinder.Ctrl.key");
		var alt = this.prefs.getBoolPref("firebinder.Alt.key");
		var shift = this.prefs.getBoolPref("firebinder.Shift.key");

		var txt = '';
		if (ctrl)
			txt = 'accel';
		if (alt)
			if (ctrl)
				txt = txt + ' alt';
			else
				txt = 'alt';
		if (shift)
			if (ctrl || alt)
				txt = txt + ' shift';
			else
				txt = 'shift';
		key.setAttribute("modifiers", txt);

		var letter = this.prefs.getCharPref("firebinder.The.key");
		key.setAttribute("key", letter);
	}
};

/** Wrap into a named function so that we can add/remove it */
function onLoadHelper(event) {
	keyHelper.startup(event);
}

window.addEventListener("load", onLoadHelper, false);
window.addEventListener("unload", function foo(e) {
	keyHelper.shutdown();
}, false);
